package com.hnld.transform;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.hnld.transform.cosntant.TransformConstant;
import com.hnld.transform.dictionary.Directory;
import com.hnld.transform.entity.Parameter;
import com.hnld.transform.enums.FileTypeEnum;
import com.hnld.transform.handler.ITypeHandler;
import com.hnld.transform.util.FindChildrenClassUtil;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class Main {

    static Map<String, ITypeHandler> typeHandlerMap = new HashMap<>();

    static {
        try {
            typeHandlerMap = FindChildrenClassUtil.findImplementations(ITypeHandler.class, "getType");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws Exception {
        //获取对象
        Parameter parameter = getObject();
        String[] citys = parameter.getCity().split(",");
        System.out.println(parameter.toString());
        for (String city : citys) {
            //创建区县目录
            String areaPath = createDirectory(parameter.getBaseTargetPath(), city, FileTypeEnum.DIRECTORY);
            for (String typeData : TransformConstant.typeDataDir) {
                //创建非图像类,图像类数据目录  这两类和原景观图像、路面图像没关系
                String typeDir = createDirectory(areaPath, typeData, FileTypeEnum.DIRECTORY);
                if (typeData.equals("非图像类数据")) {
                    continue;
                }
                //获取路线，创建路线文件夹
                typeHandlerMap.get(typeData).handleTypeData(typeDir, city, parameter);
            }
            //写入DR缺失的路线数据
            String DRPath = createDirectory(areaPath, "DR缺失路线.txt", FileTypeEnum.FILE);
            FileUtil.writeLines(TransformConstant.DR_ROUTE, DRPath, Charset.defaultCharset());

            //写入IRI缺失的路线数据
            String IRIPath = createDirectory(areaPath, "IRI缺失路线.txt", FileTypeEnum.FILE);
            FileUtil.writeLines(TransformConstant.IRI_ROUTE, IRIPath, Charset.defaultCharset());

            //写入LBI缺失的路线数据
            String LBIPath = createDirectory(areaPath, "LBI缺失路线.txt", FileTypeEnum.FILE);
            FileUtil.writeLines(TransformConstant.LBI_ROUTE, LBIPath, Charset.defaultCharset());

            //写入RI缺失的路线数据
            String RIPath = createDirectory(areaPath, "RI缺失路线.txt", FileTypeEnum.FILE);
            FileUtil.writeLines(TransformConstant.RI_ROUTE, RIPath, Charset.defaultCharset());
        }
    }

    public static Parameter getObject() {
        String filePath = "D:\\lida.json";//配置参数
        String jsonContent = null;
        Parameter parameter = null;
        try {
            jsonContent = new String(Files.readAllBytes(Paths.get(filePath)));
            parameter = JSONObject.parseObject(jsonContent, Parameter.class);
        } catch (IOException e) {
            System.out.println("获取配置文件出错");
            throw new RuntimeException(e);
        }
        return parameter;
    }

    private static String createDirectory(String path,String file, FileTypeEnum type) {
        return Directory.createDirectory(path, file, type);
    }



}
